#include "server.h"
#include "log.h"

#include <memory.h>
#include <stdlib.h>

char g_server_run_flag = 1;

void* main_content(void* server_thread)
{
    rpc_server_t* server_info = (rpc_server_t*)(server_thread);
    switch (server_info->config->transmission_type) {
        case TRANSMISSION_TCP:
            break;
    }
    while(g_server_run_flag) {
        
    }
}

void* worker_content(void* worker_info)
{
    worker_info_t* worker = (worker_info_t*)(worker_info);
    while(g_server_run_flag) {
        pthread_cond_wait(&(worker->cond), &(worker->mutex));
    }
}
worker_info_t* worker_create(const server_config_t* config)
{
    // 申请数据空间
    worker_info_t* execute_thread = (worker_info_t*)malloc(sizeof(worker_info_t));
    if (execute_thread == NULL) {
        LOG_ERROR("malloc server thread failed");
        return NULL;
    }
    (void)memset(execute_thread, 0, sizeof(worker_info_t));
    // 初始化线程状态变量
    pthread_cond_init(&(execute_thread->cond), NULL);
    pthread_mutex_init(&(execute_thread->mutex), NULL);
    // 创建执行任务队列
    execute_thread->tasks = message_queue_create(config->tasks_max_size);
    if (execute_thread->tasks == NULL) {
        LOG_ERROR("malloc tasks queue failed");
        goto WORKER_CREATE_FAILED;
    }
    // 创建worker
    int ret = pthread_create(&(execute_thread->worker), \
        NULL, worker_content, (void*)execute_thread);
    if (ret != 0) {
        LOG_ERROR("create thread failed, err: %x", ret);
        goto WORKER_CREATE_FAILED;
    }
    pthread_detach(execute_thread->worker);

    return execute_thread;

WORKER_CREATE_FAILED:
    worker_destroy(execute_thread);
    return NULL;
}

void worker_destroy(worker_info_t* worker)
{

}

rpc_server_t* rpc_server_create(server_config_t* config)
{
    // 申请server空间
    rpc_server_t* server = (rpc_server_t*)malloc(sizeof(rpc_server_t));
    if (server == NULL) {
        LOG_ERROR("malloc rpc server failed");
        return NULL;
    }
    // server初始化为0
    (void)memset(server, 0, sizeof(rpc_server_t));
    // 将config复制给server
    server->config = config;
    // 创建执行workers_info
    int i = 0;
    for (i = 0; i < EXECUTE_THREAD_NUM; i++) {
        server->workers_info[i] = worker_create(config);
        if (server->workers_info[i] == NULL) {
            goto RPC_SERVER_CREATE_FAILED;
        }
    }
    // 初始化products
    server->products = message_queue_create(config->products_max_size);
    if (server->products == NULL) {
        LOG_ERROR("malloc products failed");
        goto RPC_SERVER_CREATE_FAILED;
    }
    // 创建传输transport
    server->transport = transmission_server_create(config->transmission_type,
        config->transmission_config);
    if (server->transport == NULL) {
        goto RPC_SERVER_CREATE_FAILED;
    }
    // 创建main
    // [1] 接受client传来的命令、heardbeat
    // [2] 监控workers thread状态
    // [3] 返回信息给client
    int err = pthread_create(&(server->main), 
        NULL, main_content, server);
    if (err != 0) {
        LOG_ERROR("create communicate thread failed, err: %x", err);
        goto RPC_SERVER_CREATE_FAILED;
        return NULL;
    }
    pthread_detach(server->main);

    LOG_INFO("rpc server create succes");

    return server;

RPC_SERVER_CREATE_FAILED:
    rpc_server_destroy(&server);
    return NULL;
}

ret_t rpc_server_start(rpc_server_t* server)
{
    return RET_SUCCESS;
}

ret_t rpc_server_destroy(rpc_server_t** server)
{
    if (*server != NULL) {
        free(*server);
        *server = NULL;
    }
    return RET_SUCCESS;
}